home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / SQLTESTG.PY < prev    next >
Encoding:
Text File  |  1999-07-28  |  2.2 KB  |  78 lines

  1. # this was used for debugging null productions (a nearly full sql grammar
  2. # is available on request).
  3.  
  4. #set this to automatically rebuild the grammar.
  5. REBUILD = 1
  6. MARSHALFILE = "SQLTEST.mar"
  7.  
  8. SELECTRULES = """
  9.   ## highest level for select statement (not select for update)
  10.   select-statement ::
  11.   @R selectR :: select-statement >> 
  12.                    SELECT 
  13.                    from-clause
  14.                    where-clause
  15.                    group-by-clause
  16.                    having-clause
  17.   ## generalized to allow null from clause eg: select 2+2
  18.   @R fromNull :: from-clause >>
  19.   @R fromFull :: from-clause >> FROM 
  20.   @R whereNull :: where-clause >>
  21.   @R whereFull :: where-clause >> WHERE 
  22.   @R groupNull :: group-by-clause >>
  23.   @R groupFull :: group-by-clause >> GROUP BY 
  24.   @R havingNull :: having-clause >> 
  25.   @R havingFull :: having-clause >> HAVING
  26.   @R unionNull :: union-clause >> 
  27.   @R unionFull :: union-clause >> UNION 
  28. """
  29.  
  30. SELECTNONTERMS = """
  31.   select-statement
  32.   all-distinct select-list table-reference-list
  33.   where-clause group-by-clause having-clause union-clause
  34.   maybe-order-by
  35.   search-condition column-list maybe-all order-by-clause
  36.   column-name from-clause
  37. """
  38. # of these the following need resolution
  39. #   (select-list) (table-reference-list) 
  40. #   (search-condition) order-by-clause (column-name) 
  41.  
  42. SELECTKEYWORDS = """
  43.   SELECT FROM WHERE GROUP BY HAVING UNION DISTINCT ALL AS 
  44. """
  45.  
  46. # test generation of the grammar
  47. def BuildSQLG():
  48.    import kjParseBuild
  49.    SQLG = kjParseBuild.NullCGrammar()
  50.    SQLG.SetCaseSensitivity(0)
  51.    SQLG.Keywords(SELECTKEYWORDS)
  52.    SQLG.Nonterms(SELECTNONTERMS)
  53.    # no comments yet
  54.    SQLG.Declarerules(SELECTRULES)
  55.    print "building"
  56.    SQLG.Compile()
  57.    print "marshaling"
  58.    outfile = open( MARSHALFILE, "w")
  59.    SQLG.MarshalDump(outfile)
  60.    outfile.close()
  61.    return SQLG
  62.  
  63. # load function
  64. def LoadSQLG():
  65.    import kjParser
  66.    print "unmarshalling"
  67.    infile = open(MARSHALFILE, "r")
  68.    SQLG = kjParser.UnMarshalGram(infile)
  69.    infile.close()
  70.    return SQLG
  71.  
  72. #### for testing
  73. if REBUILD:
  74.    SQLG0 = BuildSQLG()
  75.    print " rebuilt SQLG0 as compilable grammar"
  76.  
  77. SQLG = LoadSQLG()
  78. print " build SQLG as reloaded grammar"